package main;

import bean.Task;
import bean.Worker;
import util.greedy.ArgMax;
import util.greedy.GSatisfy;

import java.util.HashSet;
import java.util.Iterator;

public class Greedy {
    public static void main(String[] args) {
        //1. 数据准备
        //工人数据
        HashSet<String> w1Skills = new HashSet<>();
        w1Skills.add("e1");
        w1Skills.add("e2");
        Worker w1 = new Worker("w1",w1Skills, 2.0,1);

        HashSet<String> w2Skills = new HashSet<>();
        w2Skills.add("e1");
        Worker w2 = new Worker("w2",w2Skills, 1.0,1);

        HashSet<String> w3Skills = new HashSet<>();
        w3Skills.add("e2");
        w3Skills.add("e3");
        Worker w3 = new Worker("w3",w3Skills, 3.0,2);

        HashSet<Worker> workers = new HashSet<>();
        workers.add(w1);
        workers.add(w2);
        workers.add(w3);

        //任务数据
        HashSet<String> taskSkills = new HashSet<>();
        taskSkills.add("e1");
        taskSkills.add("e2");
        taskSkills.add("e3");
        Task task = new Task(taskSkills);

        greedy(workers,task);
    }

    /**
     * Top-1 贪心算法
     * @param workers 工人集合
     * @param task 一个任务
     * @return team组建好的团队
     */
    public static HashSet<Worker> greedy(HashSet<Worker> workers,Task task) {

        HashSet<Worker> team = new HashSet<>();
        while ( !GSatisfy.isGSatisfy(team,task.getSkills()) ) { /*因为这个循环是死循环，所以程序停不下来*/
            HashSet<Worker> teamBreak = new HashSet<>();
            Iterator<Worker> iteratorWorkers = workers.iterator();
            while ( iteratorWorkers.hasNext() ) {
                teamBreak.add(iteratorWorkers.next());
            }

            if ( !GSatisfy.isGSatisfy(teamBreak,task.getSkills()) ) {
                break;
            }

            Worker workerNew = new Worker();
            workerNew = ArgMax.argMax(workers,task,team);
            if (workerNew != null){
                team.add(workerNew);
            }
        }
        for (Worker w : team) {
            System.out.println(w);
        }
        System.out.println("=====================");
        return team;
    }
}
